import java.util.ArrayList;
import java.util.Random;


public class DDAHelper {
	
	public double enemypoint;
	public double oldenemypoint;
	public double towerpoint;
	public double oldtowerpoint;
	public String difficulty;
	
	public double enemySpeed = 2;
	public int enemyNumber = 10;
	public double enemyHP = 50;
	public int enemyReward = 10;
	
	public double towerRange = 100;
	public double towerCD = 1;
	public double towerDamage = 10;
	public double towerSkillNumber = 0;
	public double towersNeeded = 20;
	public int towerPrice = 25;
	
 	public DDAHelper(int currentlife, int totallife, double enemypoint, double towerpoint,double towersNeeded){
 		oldtowerpoint = towerpoint;
 		oldenemypoint = enemypoint;
 		this.towersNeeded = towersNeeded;
 		double portion = (double)currentlife/(double)totallife;
 		if(portion>0.8){
 			this.enemypoint = 1.5*oldenemypoint;
 			this.towerpoint = oldtowerpoint;
 			this.towersNeeded = this.towersNeeded *0.9;
 			if(this.towersNeeded<5)
 				this.towersNeeded = 5;
 			System.out.println("Tower needed:" + this.towersNeeded);
 		}
 		else if(portion>0.5){
 			this.enemypoint = 1.05*oldenemypoint;	
 			this.towerpoint = oldtowerpoint;
 		}
 		else{
 			this.enemypoint = 1*oldenemypoint;	
 			this.towerpoint = 2*oldtowerpoint;
 			towersNeeded = towersNeeded *1.1;
 		}
	}
 	
 	public void setEnemyConfig () {
 		Random random = new Random();
 		double speedWeight = random.nextDouble();
 		double numberWeight = random.nextDouble();
 		double healthWeight = random.nextDouble();
 		double totalWeight = speedWeight + numberWeight + healthWeight;
 		
 		double speedPoint = this.enemypoint * speedWeight / totalWeight;
 		speedPoint = speedPoint > 150 ? 150 : speedPoint;
 		double numberPoint = this.enemypoint * numberWeight / totalWeight;
 		numberPoint = numberPoint > 600 ? 600 : numberPoint;
 		double healthPoint = this.enemypoint - speedPoint - numberPoint;
 		
 		this.enemySpeed = speedPoint <= 80 ? (int)(speedPoint * 0.05 + 1) : 5;
 		this.enemyNumber = numberPoint <= 150 ? (int)(numberPoint * 0.2 + 20) : 50;
 		this.enemyHP = (int)(healthPoint * 1.25 + 50);
 		System.out.println("speedPoint" + speedPoint);
 		System.out.println("numberPoint" + numberPoint);
 		System.out.println("healthPoint" + healthPoint);

 	}
 	
 	public void setTowerConfig () {
 		Random random = new Random();
 		double rangeWeight = random.nextDouble();
 		double cdWeight = random.nextDouble();
 		double damageWeight = random.nextDouble();
 		double skillWeight = random.nextDouble();
 		double totalWeight = rangeWeight + cdWeight + damageWeight + skillWeight;
 		
 		double rangePoint = this.towerpoint * rangeWeight / totalWeight;
 		rangePoint = rangePoint > 2000 ? 2000 : rangePoint;
 		double cdPoint = this.towerpoint * cdWeight / totalWeight;
 		cdPoint = cdPoint > 900 ? 900 : cdPoint;
 		
 		int p = random.nextInt(30);
 		double damagePoint = (this.towerpoint - rangePoint - cdPoint) * (p / 100 + 0.4);
 		double skillPoint = this.towerpoint - rangePoint - cdPoint - damagePoint;
 		
 		this.towerRange = rangePoint <= 1500 ?(rangePoint / 15 + 100) : 200;
 		this.towerCD = cdPoint <= 600 ? (3-cdPoint / 300) : 1;
 		this.towerDamage =(damagePoint/10);
 		double t = skillPoint / this.towerpoint;
 		if (t <= 0.1) {
 			this.towerSkillNumber = 0;
 		} else if (t <= 0.3) {
 			this.towerSkillNumber = 1;
 		} else if (t <= 0.4) {
 			this.towerSkillNumber = 2;
 		} else if (t <= 0.5) {
 			this.towerSkillNumber = 3;
 		} else if (t > 0.5) {
 			this.towerSkillNumber = 4;
 		}
 		
 		this.towerPrice = (int) ((double)this.enemyNumber *(double)this.enemyReward / this.towersNeeded);
 	}
 	
	public BasicEnemy createNewEnemy(){
 		this.setEnemyConfig();
 		this.setTowerConfig();
		BasicEnemy be =  new BasicEnemy();
		be.hp = enemyHP;
		be.vx = enemySpeed;
		be.v = enemySpeed;
		be.totalNumber = enemyNumber;
		be.reward = this.enemyReward;
		return be;
	}
	
	public ArrayList<BasicTower> createTowers(boolean copyold){
		int towerNumber = 4;
		if(copyold)
			towerNumber = 3;
		ArrayList<BasicTower> newTowerList = new ArrayList<BasicTower>();
		int[][] colours = new int[][]{new int[] {255, 255, 0},  new int[] {255, 0, 0},new int[] {0, 255, 0},new int[] {0, 0, 255}};
		for(int i=0; i<towerNumber;i++){
			ArrayList<String> skillsList = new ArrayList<String>();
			Random random = new Random();
			skillsList.add(Global.Laser);
			skillsList.add(Global.Transmit);
			skillsList.add(Global.Seckilling);
			skillsList.add(Global.Dot);
			skillsList.add(Global.Freeze);
			
			setTowerConfig ();
			BasicTower bt = new BasicTower();
			bt.ap = towerDamage;
			bt.range = towerRange;
			bt.heat = towerCD;
			bt.cost = this.towerPrice;
			WeaponComponent wc  = new WeaponComponent();
			wc.name = Global.SingleAttack;
			wc.heat = towerCD;
			wc.attackdamage = towerDamage;
			bt.weaponComponentList.add(wc);
			
			for(int j=0;j<towerSkillNumber;j++){
				int index = random.nextInt(skillsList.size());
				if(skillsList.get(index)==Global.Laser){
					wc  = new WeaponComponent();
					wc.name = Global.Laser;
					wc.heat = towerCD;
					wc.attackdamage = towerDamage*0.5;
					bt.cost += 3;
				}
				else if(skillsList.get(index)==Global.Transmit){
					wc  = new WeaponComponent();
					wc.name = Global.Transmit;
					wc.heat = towerCD*8;
					bt.cost +=5;
				}
				else if(skillsList.get(index)==Global.Seckilling){
					wc  = new WeaponComponent();
					wc.name = Global.Seckilling;
					wc.heat = towerCD*8;
					bt.cost +=5;
				}
				else if(skillsList.get(index)==Global.Dot){
					wc  = new WeaponComponent();
					wc.name = Global.Dot;
					wc.heat = towerCD*1.2;
					wc.dotdamage =  towerDamage*0.4;
					wc.dottimer = 10;
					bt.cost += 1;
				}
				else{
					wc  = new WeaponComponent();
					wc.name = Global.Freeze;
					wc.heat = towerCD*1.5;
					wc.freeze = towerCD/2;
					bt.cost += 2;
				}
				skillsList.remove(index);
				bt.weaponComponentList.add(wc);
				bt.colours = colours[i];
			}
			
			newTowerList.add(bt);
		}
		
		return newTowerList;
	}
}
